import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import '../themes/app_theme.dart';

class ThemeController extends GetxController {
  final _box = GetStorage();

  var isDarkMode = false.obs;
  var primaryColor = Colors.red[600]!.obs;

  static const _keyThemeMode = 'theme_mode';
  static const _keyPrimaryColor = 'primary_color';

  @override
  void onInit() {
    super.onInit();
    isDarkMode.value = _box.read(_keyThemeMode) ?? false;

    int colorValue = _box.read(_keyPrimaryColor) ?? Colors.blue.value;
    primaryColor.value = Color(colorValue);
  }

  ThemeData get theme {
    return isDarkMode.value
        ? AppTheme.darkTheme(primaryColor.value)
        : AppTheme.lightTheme(primaryColor.value);
  }

  void toggleTheme() {
    isDarkMode.value = !isDarkMode.value;
    _box.write(_keyThemeMode, isDarkMode.value);
  }

  void changePrimaryColor(Color color) {
    primaryColor.value = color;
    _box.write(_keyPrimaryColor, color.value);
  }
}
